<?php
apf_require_class("Bll_BLL");
class Bll_Attendance_Unaudit extends BLL{

    private $user_info;
    private $boss_id;
    /**
     * 管理员根据id获取
     */
    public function get_list($user_id,$page,$offset,$where="",$start,$end){
        //是否有查找
        $who = trim($where);
        if($start !='' && $end !=''){
            $start = $start.' 00:00:00';
            $end = $end.' 23:59:59';
            $start = trim($start);
            $end = trim($end);
        //$start = $start.' 00:00:00';
        //$end = $end.' 23:59:59';
            $and = " and ((l.start_time>='{$start}' and l.start_time<='{$end}') or ( l.end_time>='{$start}' and l.end_time<='{$end}'))";
        }
        else{
            $and = "";
        }
        if($who !=""){
            $and  .= " and ((u.name like '%{$where}%') or u.code like '%{$where}%')";
        }
        else{
            $and .= '';
        }
        //对hr做特殊处理
        apf_require_class("Bll_Attendance_UserInfo");
        $bll_user = new Bll_Attendance_UserInfo();
        $role = $bll_user->get_user_role($user_id);
        if($role[$user_id]['role_id'] == 3 && !empty($who)){
            $sql = "select u.name,u.code,u.department_name,u.department_id,l.start_time,l.end_time,l.type,l.id,l.leave_reason,l.status,l.leave_desc,l.r_user_id,l.leave_time
                from leave_record l left join
                user_info u on l.user_id = u.user_id
                where l.status != '未审核' AND l.status != '已撤销' $and
                order by l.id desc
                limit $offset,$page
                ";
        } else {
            $sql = "select a.*,u.user_id,u.name,u.code,u.department_name,u.department_id,l.start_time,l.end_time,l.type,l.id,l.leave_reason,l.status,l.leave_desc,l.r_user_id,l.leave_time
            from audit_list a left join
            leave_record l on a.leave_id = l.id left join
            user_info u on l.user_id = u.user_id
            where a.uid = $user_id and a.status = 2 and l.status <> '已撤销' $and
            order by a.leave_id desc
            limit $offset,$page";
        }
        $rows = $this->get_defaut_dao()->find_by_sql($sql);
        if($role[$user_id]['role_id'] == 3 && !empty($who)){
            $sql_count = "select count(l.id) as count
                from leave_record l left join
                user_info u on l.user_id = u.user_id
                where l.status != '未审核' AND l.status != '已撤销' $and";
            //如果是HR搜索时
            $data['uroles'] = $role[$user_id]['role_id'];
        } else {
            $sql_count = "select count(a.uid) as count
            from audit_list a left join
            leave_record l on a.leave_id = l.id left join
            user_info u on l.user_id = u.user_id
            where a.uid = $user_id and a.status = 2 and l.status <> '已撤销' $and";
        }
        $data['rows'] = $rows;
        $count = $this->get_defaut_dao()->find_by_sql($sql_count);
        $data['count'] = $count[0]['count'];
        /*
        //获取BOSS id
        $this->get_boss_id();
        //判断是否为BOSS
        if($user_id != $this->boss_id){
            $or = " or (leave_time >=3 and r.b_user_id = $user_id) ";
        }
        else{
            $or = ' ';
        }

        //查询出差，每个部门都是独立的
        $adut_info = $this->get_trip_manage_ids($user_id);
        if(empty($adut_info)){
            $bigor = ' ';
        }
        else{
            $ids = '';
            foreach($adut_info as $s){
                $ids .= $s['department_id'].",";
            }
            $ids = rtrim($ids,",");
            $bigor = " or ( r.status = '已批准' and r.type ='出差' and u.department_id in({$ids})) ";
        }

        $sql = "SELECT rl.*,u.name,u.code,u.department_id,u.department_name,r.id,r.r_user_id,r.b_user_id,r.leave_time,r.type,r.leave_reason,r.start_time,r.end_time
                from leave_record r LEFT JOIN
                user_info u on r.user_id = u.user_id LEFT JOIN
                leave_record_log rl on rl.leave_id = r.id
                where
                !(r.leave_reason='地铁故障' and r.type='迟到') and
                ((r.type !='出差' and (r.r_user_id = $user_id $or ) and rl.operator = $user_id )  $bigor) $and
                order by r.apply_time desc
                limit $offset,$page";
        //var_dump($sql);
        $rows = $this->get_defaut_dao()->find_by_sql($sql);
        //var_dump($rows);

        foreach($rows as &$v){
            if($v['b_user_id'] == $user_id and $v['type'] !='出差'){
                $v['is_second'] = 1;
            }
        }

        $sql_count = "SELECT count(r.id) as count
                from leave_record r LEFT JOIN
                user_info u on r.user_id = u.user_id
                LEFT JOIN
                leave_record_log rl on rl.leave_id = r.id
                where
                !(r.leave_reason='地铁故障' and r.type='迟到') and
                ((r.type !='出差' and (r.r_user_id = $user_id $or ) and rl.operator = $user_id) $bigor) $and ";

        $count = $this->get_defaut_dao()->find_by_sql($sql_count);
        $data['rows'] = $rows;
        $data['count'] = $count[0]['count'];
        */
        return $data;
    }

    public function get_record_log($user_id){
        $rows = $this->get_leaverecordlog_dao()->find(array('operator'=>$user_id));
        return $rows;
    }


    //根据leave_id 获取请假状态信息
    public function get_leave_info($leave_id){
        $rows = $this->get_defaut_dao()->find_row(array('id'=>$leave_id));
        return $rows;
    }

    private function get_trip_manage_ids($user_id){
        $ids = $this->get_tripmanager_dao()->find(array('user_id'=>$user_id));
        return $ids;
    }

    private function get_boss_id(){
        //TODO 可配置code
        $code = "SHA001";
        $row = $this->get_userinfo_dao()->find_row(array('code'=>$code));
        $this->boss_id = $row['user_id'];
        return $row['user_id'];
    }


    private function get_user_info(){
        $this->session_act();
        $this->user_info = $_SESSION;
        return $this->user_info;
    }

    /**
     *
     */
    private function get_defaut_dao(){
        return $this->get_dao('DAO_Attendance_LeaveRecord');
    }
    private function get_tripmanager_dao(){
        return $this->get_dao('DAO_Attendance_TripManager');
    }
    private function get_userinfo_dao(){
        return $this->get_dao('DAO_Attendance_UserInfo');
    }
    private function get_leaverecordlog_dao(){
        return $this->get_dao('DAO_Attendance_LeaveRecordLog');
    }
}
